package com.ks3.demo.main;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.ksyun.ks3.exception.Ks3ClientException;
import com.ksyun.ks3.exception.Ks3ServiceException;
import com.ksyun.ks3.model.result.CompleteMultipartUploadResult;
import com.ksyun.ks3.services.Ks3Client;
import com.ksyun.ks3.services.MultiUploader;
import com.ksyun.ks3.services.handler.CompleteMultipartUploadResponseHandler;
import com.ksyun.ks3.services.request.CompleteMultipartUploadRequest;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MultiUploadActivity extends Activity {
    final private String TAG = "MultiUploader";
    private List<MultiUploader> fileInfos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_multi_upload);
        getFileInfo();
        ListView listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(new ListViewAdapter(fileInfos));
    }

    private void getFileInfo() {
        fileInfos = new ArrayList<MultiUploader>();
        Ks3Client client = Ks3ClientFactory.getDefaultClient(MultiUploadActivity.this);
        fileInfos.add(new MultiUploader(client, Constants.TEST_MULTIUPLOAD_BUCKET, "pdf.pdf",
                new File(Constants.TEST_MULTIUPLOAD_FILE)));//, "bb20153e438047b3bc3212f37e25fd9c"));
        fileInfos.add(new MultiUploader(client, Constants.TEST_MULTIUPLOAD_BUCKET, "reload-pdf.pdf",
                new File(Constants.TEST_MULTIUPLOAD_FILE), "7ab054f4c6b44a9cadb163a82ae05a54"));
    }

    public class ListViewAdapter extends BaseAdapter {
        View[] itemViews;

        public ListViewAdapter(List<MultiUploader> list) {
            itemViews = new View[list.size()];
            for (int i = 0; i < list.size(); i++) {
                MultiUploader uploader = list.get(i);
                itemViews[i] = makeItemView(uploader.getKey(), uploader);
            }
        }

        private View makeItemView(String key, final MultiUploader uploader) {
            LayoutInflater inflater = (LayoutInflater) MultiUploadActivity.this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // 使用View的对象itemView与R.layout.item关联
            View itemView = inflater.inflate(R.layout.multiupload_item, null);
            TextView fileNameView = (TextView) itemView.findViewById(R.id.file_name);
            fileNameView.setText(key);
            Button btn = (Button) itemView.findViewById(R.id.uploadBtn);
            if (uploader.getUploadId() == null) {
                btn.setText("分块上传");
            } else {
                btn.setText("分块续传");
            }
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    uploader.setHandler(new CompleteMultipartUploadResponseHandler() {
                        @Override
                        public void onFailure(CompleteMultipartUploadRequest request, Ks3ClientException clientException, Ks3ServiceException serviceException) {
                            if (clientException != null) {
                                Log.e("", "complete upload fail, clientException=" + clientException.getMessage());
                            }
                            if (serviceException != null) {
                                Log.e(TAG, "complete upload fail, serviceException=" + serviceException.getMessage());
                            }
                            ToastUtils.showShort("分块上传合并失败！");
                        }

                        @Override
                        public void onSuccess(CompleteMultipartUploadRequest request, CompleteMultipartUploadResult result) {
                            Log.i(TAG, "complete upload success, status code=" + result.getStatusCode());
                            ToastUtils.showShort("分块上传成功！");
                        }
                    });
                    ToastUtils.showShort("分块上传开始！");
                    if (uploader.getUploadId() == null)
                        uploader.upload();
                    else
                        uploader.reUpload();
                }
            });
            TextView result = itemView.findViewById(R.id.result);
            result.setText("");
            return itemView;
        }

        @Override
        public int getCount() {
            return itemViews.length;
        }

        @Override
        public Object getItem(int i) {
            return itemViews[i];
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            if (view == null)
                return itemViews[i];
            return view;
        }
    }
}
